package com.sisyphe.android.srouter.compiler.builder

import com.sisyphe.android.srouter.facade.annotation.Route
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.MAP
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.buildCodeBlock
import javax.annotation.processing.Filer
import javax.annotation.processing.Messager
import javax.lang.model.element.Element

class RouteBuilderImpl(private val filer: Filer, private val messager: Messager) : IRouteBuilder {

    private val routeSet = arrayListOf<String>()

    override fun collect(element: Element) {
        val route = element.getAnnotation(Route::class.java)
        val routePath = route.path

        routeSet.add(routePath)

//        val simpleName = "SRouter__${element.simpleName}"
//
//        val packageName = element.enclosingElement.toString()
//
//        val typeSpec = TypeSpec.classBuilder(ClassName(packageName, simpleName))
//            .build()
//
//        FileSpec.builder(packageName, simpleName)
//            .addType(typeSpec)
//            .build()
//            .writeTo(filer)
//
//        val route = element.getAnnotation(Route::class.java)
//        val path = route.path
//
//        if (path.isNotEmpty()) {
//            val pathGroup = path.split("/").filter { it.isNotEmpty() }
//            val name = route.name
//            val group = route.group
//
//            if (pathGroup.isNotEmpty()) {
//                if (group.isNotEmpty()) {
//
//                } else {
//                    if (1 < pathGroup.size) {
//                        val groupAlias = pathGroup[0]
//                    } else {
//                        messager.printMessage(Diagnostic.Kind.ERROR, "The path value from Route annotation should be '/xx/xx' or set the group value.", element)
//                    }
//                }
//            }
//        } else {
//            messager.printMessage(Diagnostic.Kind.ERROR, "The path value from Route annotation can't be empty.", element)
//        }
    }

    override fun build() {
        FileSpec.builder("com.sisyphe.android.srouter.collections", "SRouter__RouteCollections")
            .addType(
                TypeSpec.classBuilder(ClassName.bestGuess("com.sisyphe.android.srouter.collections.SRouter__RouteCollections"))
                    .addModifiers(KModifier.FINAL)
                    .addProperty(
                        PropertySpec.builder("routeMapList", MAP.plusParameter(STRING).plusParameter(STRING), KModifier.PRIVATE)
                            .initializer(
                                buildCodeBlock {
                                    add("mutableMapOf(")
                                    routeSet.forEach {
                                        add("\"$it\" to \"\", ")
                                    }
                                    add(")")
                                }
                            )
                            .build()
                    )
                    .build()
            )
            .addFileComment("GENERATED BY BUILD, DON,T MODIFY.")
            .build()
            .writeTo(filer)
    }
}